// pages/member_card/card_list/card_list.js
var api = require('../../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSelectBox: false,
    showSelectOptions: false,
    step: 1,
    longitude: '',
    latitude: '',
    lists: [],
    page: 1,
    lastPage: 0,
    selectIndex: 0,
    carLists: [],
    selectCar: {
      plate_number: '请选择车辆',
      id: 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('Location')) {
      let location = wx.getStorageSync('Location');
      this.setData({
        latitude: location.latitude,
        longitude: location.longitude
      })
    }
    this.getCard()
    this.getUserCar();
  },
  getUserCar() {
    api.get('/user/cars').then(res => {
      this.setData({
        carLists: res.items
      })
    })
  },
  selctCard(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index;
    this.setData({
      selectIndex: index
    })
  },
  getCard() {

    if (this.data.lastPage != 0 && this.data.page >= this.data.lastPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return false;
    }
    api.get('/card/index', {
      longitude: this.data.longitude,
      latitude: this.data.latitude,
      page: this.data.page
    }).then(res => {
      this.setData({
        lists: res.items,
        lastPage: res.lastPage,
        page: this.data.page + 1
      })
    })
  },
  showSelectOtionsFun() {
    this.setData({
      showSelectOptions: !this.data.showSelectOptions
    })
  },
  showSlectBox() {
    this.setData({
      showSelectBox: true
    })
  },

  closeBox() {
    this.setData({
      showSelectBox: false,
      showSelectOptions: false
    })
  },
  selctCar(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index;
    this.setData({
      selectCar: this.data.carLists[index],
      showSelectOptions: false
    })
  },
  pay() {
    if(!this.data.selectCar || !this.data.selectCar.id){
      wx.showToast({
        title: '请选择车辆',
        icon:'none'
      })
      return false;
    }
    let that = this;
    api.post('/card/pay', {
      card_id: this.data.lists[this.data.selectIndex].id,
      car_id: this.data.selectCar.id,
    }).then(res => {

      wx.requestPayment({
        ...res.payInfo,
        success: function (res) {
          console.log('success:' + JSON.stringify(res));
          that.setData({
            step: 2
          })
        },
        fail: function (err) {
          console.log('fail:' + JSON.stringify(err));
          if (err.errMsg == "requestPayment:fail cancel") {
            wx.showToast({
              title: '取消支付',
              icon: 'none',
              duration: 3000
            })
          } else {
            wx.showToast({
              title: JSON.stringify(err),
              icon: 'none',
              duration: 3000
            })
          }
        }
      });


    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})